package wangbaguanlixitong.servlet;

import wangbaguanlixitong.dao.AdminDao;
import wangbaguanlixitong.model.Admin;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet(name = "LoginServlet",urlPatterns = "/loginSer")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置编码格式 会乱码
        response.setContentType("text/html;charset=utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //判断用户名、密码是否为空
        if(username==""||username.length()==0){
            request.setAttribute("namemsg","用户名为空!");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }
        if(password==""||password.length()==0) {
            request.setAttribute("pwdmsg", "密码为空!");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }
        //判断账号和密码的正确性
        AdminDao adminDao = new AdminDao();
        Admin admin = adminDao.selectUser(username,password);
        if (admin.getUserName()!=null){
            request.getRequestDispatcher("main.jsp").forward(request,response);
        }else {
            request.setAttribute("info","账号未注册");
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }
//        try {
//            if(rs.next()){
//                if(username.equals(rs.getString(username))&&password.equals(rs.getString(password))){
//                 /*
//                  request.setAttribute("username",username);
//                    //将用户user存入session域
//                    Admin admin = new Admin();
//                    admin.setUserName(username);
//                    admin.setUserPw(password);
//                    request.getSession().setAttribute("Admin",admin);
//
//                  */
//
//                    request.getRequestDispatcher("main.jsp").forward(request,response);
//                }else{
//                    request.setAttribute("pwdError","密码不正确!");
//                    request.getRequestDispatcher("login.jsp").forward(request, response);
//                }
//            }else{
//                request.setAttribute("nameError","用户名不存在!");
//                request.getRequestDispatcher("login.jsp").forward(request, response);
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
    }



    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         request.getRequestDispatcher("login.jsp").forward(request,response);
    }
}
